# coding=utf8

from math import *
import glgui
from pyglet.gl import *
import pyglet
import random
import squirtle
import glgui2
import Map
import bz2
import ctypes
import cairo

label = None
hexa = None
mk = None
style = None
layout = None
canvas1 = None
mpos = [100,100]
arrow = None
signaler = None

class guiMainMenu():
  
    def start(self):
        self.scroll.visible = not self.scroll.visible
    def quit(self):
        print 'trololo'
    def val(self,val):
        print val
    def __init__(self):
        glgui2.signal = glgui2.Signal(window)
        self.layout = glgui2.Layout((1024,768))
        #self.frame = glgui2.ScrollableLayout('frame',pos = (200,200), size=(200,200),viewsize=(400,400),layout = glgui2.CentralLayout((200,200)))
        tg = pyglet.image.ImageGrid(pyglet.image.load('graphics/hextiles.png'),1,8)
        self.frame = glgui2.Frame('test',size=(400,400),layout = glgui2.CentralLayout((400,400)))
        tt = glgui2.HListBox('Listbox',[tg[0],tg[1]],(0,0),(240,36))
        self.frame.layout.add_widget(tt)
        
        tt.set_elements([tg[0],tg[1],tg[2]])
        
        #~ self.layout.add_widget(self.frame2)
        self.frame.visible = True
        self.frame.update_rect()

        #~ self.frame.update_rect()
        #~ self.frame = glgui2.Frame('test',size=(200,200),layout = glgui2.CentralLayout((200,200)))
        
        
        #~ self.frame.layout.add_widget(glgui2.Button('test1','bets'))
        #~ self.frame.layout.add_widget(glgui2.Button('test2','bets'))
        #~ self.frame.layout.add_widget(glgui2.Button('test3','bets'))
        #~ self.frame.layout.add_widget(glgui2.Button('test4','bets'))
        #~ self.frame.layout.add_widget(glgui2.Button('test5','bets'))
        
        self.frame.layout.add_widget(glgui2.Combobox('testt2',['Terrans','Zerg','Protos']))
        self.frame.layout.add_widget(glgui2.Combobox('test3',['Terranx','Zerg','Protos']))
        
        self.layout.add_widget(self.frame)
        self.frame.update_rect()
        
        #glgui2.common_signals.connect_to_channel("gui_events/on_value_changed/sct",self.val)
        self.layout.update_rect()
        
        
        #~ self.frame.x +=320
        #~ self.frame.x -=320
        
        self.frame.update_rect()
    
    def draw(self):
        self.layout.draw()
        
import hexfield


screen = (1024,768)
try:
    # Try and create a window with multisampling (antialiasing)
    config = Config(sample_buffers=0, samples=0, 
                    depth_size=16, double_buffer=True,)
    window = pyglet.window.Window(resizable=False, config=config,width = screen[0],height = screen[1])
    
    signaler = glgui.HierarchicalSignal(window,(0,0,1024,768))
    
    document = pyglet.text.document.FormattedDocument()
    #layout = pyglet.text.layout.IncrementalTextLayout(document, 640, 480)
    #caret = pyglet.text.caret.Caret(layout)
    #window.push_handlers(caret)
    
except pyglet.window.NoSuchConfigException:
    # Fall back to no multisampling for old hardware
    window = pyglet.window.Window(resizable=True)



        
@window.event
def on_update(dt):
    print update
    hexa.update(dt)
@window.event
def on_resize(width, height):
    # Override the default on_resize handler to create a 3D projection
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,screen[0],0,screen[1],0.1,10000.0)
    #gluPerspective(60., width / float(height), .1, 1000000.)
    glMatrixMode(GL_MODELVIEW)
    return pyglet.event.EVENT_HANDLED

@window.event
def on_draw():
    hexa.update(0)
    glClearColor(0.5,0.7,0.9,0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glDisable(GL_DEPTH_TEST)
    glTranslatef(0, 0, -1)
    glEnable(GL_COLOR_MATERIAL)
    fps_display.draw()
    layout.draw()
    glColor4f(1.0,1.0,1.0,1.0)
   
def setup():
    global hexa
    hexa = hexfield.Hexfield('field','graphics/hextiles.png',8,(32,32))
    hexa.build()
    hexa.set_opacity(180)
    # One-time GL setup
    glClearColor(0, 0, 0, 1)
    #glEnable(GL_DEPTH_TEST)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable(GL_COLOR_MATERIAL)
    global mk,label,image,texture_grid,hex,batch,layout,style,canvas1
    layout = guiMainMenu()

    
def draw(self):
    pass

setup()

fps_display = pyglet.clock.ClockDisplay()
pyglet.app.run()
